package ch10_thread.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 第一种：运行结果出错
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class MultiThreadError implements Runnable {
    public static void main(String[] args) throws Exception {
        MultiThreadError th = new MultiThreadError();
        Thread t01 = new Thread(th);
        Thread t02 = new Thread(th);
        t01.start();
        t02.start();
        // 确保两个子线程执行完了输出i
        t01.join();
        t02.join();
        System.out.println(th.i);
        System.out.println(th.aa);
    }

    // 普通变量
    int i = 0;
    // 原子变量
    AtomicInteger aa = new AtomicInteger();

    @Override
    public void run() {
        for (int j = 0; j < 10000; j++) {
            aa.incrementAndGet();
            i++;
        }
    }
}
